package com.example.jbmsuppliers.sinopec.net;

import android.os.Bundle;
import android.os.Handler;

import com.jbm.jbmsupplier.util.IAppVersion;
import com.jbm.jbmsupplier.util.ICertAddress;
import com.jbm.jbmsupplier.util.ICtrl;
import com.jbm.jbmsupplier.util.IEGLogCtrl;
import com.jbm.jbmsupplier.util.IEGuardKey;
import com.jbm.jbmsupplier.util.IEGuardKeyCtrl;
import com.jbm.jbmsupplier.util.IGeoinfo;
import com.jbm.jbmsupplier.util.IGeoinfoCtrl;
import com.jbm.jbmsupplier.util.IVerifyCode;
import com.jbm.jbmsupplier.util.Login;
import com.jbm.jbmsupplier.util.LoginEntity;
import com.jbm.jbmsupplier.util.abstracts.SendMessageInterfase;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.example.jbmsuppliers.sinopec.activity.LoginActivity.CMD_WXKEYLOGIN;
import static com.example.jbmsuppliers.sinopec.activity.LoginActivity.WXMESSFILL;
import static com.example.jbmsuppliers.sinopec.activity.MyAddressAll.LOOKADDRESSMESS;
import static com.example.jbmsuppliers.sinopec.activity.MyDoorManageF.DISTRADDRESSMESSAGE;
import static com.example.jbmsuppliers.sinopec.activity.MyMabageDoor.OPDE_DOORS;
import static com.example.jbmsuppliers.sinopec.activity.NewHomeActivity.OPDE_DOOR;
import static com.example.jbmsuppliers.sinopec.activity.PhotoLoginPwass.IMPORT_PWA_PHOTO;
import static com.example.jbmsuppliers.sinopec.activity.RegisterActivity.WX_BINGS;
import static com.example.jbmsuppliers.sinopec.activity.RegisterActivity.WX_CODE_VERS;
import static com.example.jbmsuppliers.sinopec.activity.RegisterPhotoActivity.PHOTO_BRID;
import static com.example.jbmsuppliers.sinopec.activity.RegisterPhotoActivity.PHOTO_GRAINVER_CODE;
import static com.example.jbmsuppliers.sinopec.activity.SplashActivity.PHOTO_LOGINS;
import static com.example.jbmsuppliers.sinopec.activity.SplashActivity.VISITION;
import static com.example.jbmsuppliers.sinopec.doorjurisd.DistributeOneActivity.Cmd_ShareEGuardKeys;
import static com.example.jbmsuppliers.sinopec.doorjurisd.DoorManageActivity.AUTHORADDRESSMESS;
import static com.example.jbmsuppliers.sinopec.doorjurisd.DoorOpenBlue.OPENKEY;
import static com.example.jbmsuppliers.sinopec.doorjurisd.EmpowerActivity.Cmd_Empowers;
import static com.example.jbmsuppliers.sinopec.doorjurisd.IdentificationStepOneActivity.Com_Add_Carno_key;
import static com.example.jbmsuppliers.sinopec.doorjurisd.IdentificationStepTwoActivity.Cmd_Add_AddressforKeys;
import static com.example.jbmsuppliers.sinopec.doorjurisd.IdentificationStepTwoActivity.Cmd_GetGeoInfos;
import static com.example.jbmsuppliers.sinopec.doorjurisd.IdentificationStepTwoActivity.Cmd_RegistKeyWithIDCards;


/**
 * Created by 家帮忙 on 2017/1/9.
 */
public class NetWork extends SendMessageInterfase {
    private Handler handler;
    private Bundle bundle;
    private IGeoinfo mGeoInfo = null;
    private  String mType = "";
    private  int mId = 0;
    private  String mCardNo = null;
    private final  String CMD_CODE = "cmd_code";
    private final  String VISION = "VISION";
    private final  String PHOTOLOGIN = "PHOTOLOGIN";
    private final String VERPHOTO="VERPHOTO";
    private final String BRIDPHOTO="BRIDPHOTO";
    private final String PHOTOPWS="PHOTOPWS";
    private final String WEIXINLOGIN="WEIXINLOGIN";
    private final String WXPHOTOCODE="WXPHOTOCODE";
    private final String WXLOGINCODE="WXLOGINCODE";
    private final String WXFILL="WXFILL";
    private final String MESSAGEDOOR="MESSAGEDOOR";
    private final String MESSAGEDOORS="MESSAGEDOORS";
    private final String LOOKADDRESS="LOOKADDRESS";
    private final String AUTHORADDRESS="AUTHORADDRESS";
    private final String DISTRADDRESS="DISTRADDRESS";
    private final String Com_Add_Carno_keys="Com_Add_Carno_key";
    private final String Cmd_GetGeoInfo="Cmd_GetGeoInfo";
    private final String Cmd_Add_AddressforKey="Cmd_Add_AddressforKey";
    private final String Cmd_RegistKeyWithIDCard="Cmd_RegistKeyWithIDCard";
    private final String Cmd_Empower="Cmd_Empower";
    private final String Cmd_ShareEGuardKey="Cmd_ShareEGuardKey";
    private final String OPENKEYIEG="OPENKEYIEG";
    private final String MODIHEAD="MODIHEAD";
    private final String MODINAME="MODINAME";
    private final String MODISEX="MODISEX";
    private final String UPDATEMES="UPDATEMES";
    private final String LOGINMOREN="LOGINMOREN0";


    private IEGLogCtrl keyieglog;
    private byte bys[];
    public NetWork(Handler handler) {
        this.handler = handler;
    }
    @Override
    protected void RunContext() {
        String cmd = bundle.getString(CMD_CODE);
        if (cmd.equals(VISION)) {//检查版本号
            IAppVersion ver = gSocket.GetLastVersion("jbm.android", bundle.getInt("star"), bundle.getInt("end"), 0, 0);
            if (ver!=null)
            handler.sendMessage(handler.obtainMessage(VISITION, ver));
        } else if (cmd.equals(PHOTOLOGIN)) {//手机号登录
            String result = gSocket.Login(bundle.getString("name"), bundle.getString("pwd"));
            if (result!=null)
            handler.sendMessage(handler.obtainMessage(PHOTO_LOGINS/*PHOTO_LOGIN*/, result));

        }else if (cmd.equals(VERPHOTO)){//手机号获取验证码
            Login photologin = gSocket.RegistCustomer(0, bundle.getString("photo"), "", "", "", false,"");
            if (photologin!=null)
                handler.sendMessage(handler.obtainMessage(PHOTO_GRAINVER_CODE, photologin));
        }else  if (cmd.equals(BRIDPHOTO)){//验证码获取成功后进行搭桥如果成功就跳转到密码设置页面
            Login photologinbrid = gSocket.RegistCustomer(bundle.getInt("regiid"), "", "", "", bundle.getString("vercpdephoto"), true,bundle.getString("code_photo"));
            if (photologinbrid!=null)
                handler.sendMessage(handler.obtainMessage(PHOTO_BRID, photologinbrid));
        }else if (cmd.equals(PHOTOPWS)){//设置密码
            Login result = gSocket.RegistCustomer(bundle.getInt("Loginid"), "", bundle.getString("photopw"), bundle.getString("photopws"), "", false,"");
            if (result!=null)
                handler.sendMessage(handler.obtainMessage(IMPORT_PWA_PHOTO, result));
        }else if (cmd.equals(WEIXINLOGIN)){//微信登录
            String wxLogin = gSocket.Login(bundle.getString("wxname"), bundle.getString("wxpws"));

            if (wxLogin!=null)
                handler.sendMessage(handler.obtainMessage(CMD_WXKEYLOGIN, wxLogin));

        }else if (cmd.equals(WXPHOTOCODE)){//微信获取验证码
            IVerifyCode result = gSocket.CreateVCode(bundle.getString("wxphotocode"), "reg_user");
            if (result!=null)
                handler.sendMessage(handler.obtainMessage(WX_CODE_VERS, result));

        }else if (cmd.equals(WXLOGINCODE)){//微信获取验证码成功后绑定的时候调用

            Login result = gSocket.RegistCustomer(0, bundle.getString("photo"), "", "", bundle.getString("proving"), 0, bundle.getString("username"), bundle.getString("nickname"), bundle.getInt("sex",0),bundle.getString("headimgurl"),bundle.getString("procode"));
            if (result!=null)
                handler.sendMessage(handler.obtainMessage(WX_BINGS, result));
        }else if (cmd.equals(MESSAGEDOOR)){//获取用户的开门key
            IEGuardKeyCtrl ctrlkey = gSocket.GetMyEGuardKey();
                handler.sendMessage(handler.obtainMessage(OPDE_DOOR,ctrlkey));

        }else if (cmd.equals(MESSAGEDOORS)){
            IEGuardKeyCtrl ctrlkey = gSocket.GetMyEGuardKey();
            handler.sendMessage(handler.obtainMessage(OPDE_DOORS,ctrlkey));

        }else if (cmd.equals(LOOKADDRESS)){//获取用户的地址信息
            IEGuardKeyCtrl ctrlkey = gSocket.GetMyEGuardKey();
            handler.sendMessage(handler.obtainMessage(LOOKADDRESSMESS,ctrlkey));

        }else if (cmd.equals(AUTHORADDRESS)){//获取用户的授权信息
            IEGuardKeyCtrl ctrlkey = gSocket.GetMyEGuardKey();
            handler.sendMessage(handler.obtainMessage(AUTHORADDRESSMESS,ctrlkey));

        }else if (cmd.equals(DISTRADDRESS)){//用户分发信息
            IEGuardKeyCtrl ctrlkey = gSocket.GetMyEGuardKey();
            handler.sendMessage(handler.obtainMessage(DISTRADDRESSMESSAGE,ctrlkey));

        }else if (cmd.equals(Com_Add_Carno_keys)){//用户卡号认证
            IEGuardKey key = gSocket.RegistKeyWithIDCard(0,this.bundle.getString("addrecar"),"","");
            handler.sendMessage(handler.obtainMessage(Com_Add_Carno_key,key));
        }else if (cmd.equals(Cmd_GetGeoInfo)){//获取用户输入的地址
            IGeoinfoCtrl ctrl = null;
            if(mGeoInfo == null)
                ctrl = gSocket.GetGeoinfo(0,0,"120100000000","");
            else {
                ctrl = gSocket.GetGeoinfo(0, mGeoInfo.gi_id, "", mType);
            }
            handler.sendMessage(handler.obtainMessage(Cmd_GetGeoInfos,ctrl));
        }else if (cmd.equals(Cmd_Add_AddressforKey)){

            ICertAddress addressCtrl = gSocket.AddMyCAddr(this.bundle.getInt("id"),"",this.bundle.getString("address"),"","","","");
            handler.sendMessage(handler.obtainMessage(Cmd_Add_AddressforKeys,addressCtrl));
        }else if (cmd.equals(Cmd_RegistKeyWithIDCard)){
            String name = this.bundle.getString("name");
            String card = this.bundle.getString("card");
            IEGuardKey key = gSocket.RegistKeyWithIDCard(mId,mCardNo,"","");
            handler.sendMessage(handler.obtainMessage(Cmd_RegistKeyWithIDCards,key));
        }else if (cmd.equals(Cmd_Empower)){//给用户授权
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String startDate = formatter.format(curDate);
            Calendar calendar= Calendar.getInstance();
            calendar.setTime(curDate);
            calendar.add(Calendar.DATE,+1);//+1今天的时间加一天
            String endDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
            String mobile = this.bundle.getString("mobile");
            IEGuardKey key = gSocket.ShareEGuardKey(this.mId,1,startDate,endDate+"",mobile,"","");
            handler.sendMessage(handler.obtainMessage(Cmd_Empowers,key));
        }else if (cmd.equals(Cmd_ShareEGuardKey)){//给用户分发
            int total = this.bundle.getInt("total");
            String openDate = this.bundle.getString("openDate");
            String expirDate = this.bundle.getString("expirDate");
            String mobile = this.bundle.getString("mobile");
//            String name = this.bundle.getString("name");
//            String card = this.bundle.getString("card");
            IEGuardKey key = gSocket.ShareEGuardKey(this.mId,total,openDate,expirDate,mobile,"","");
            handler.sendMessage(handler.obtainMessage(Cmd_ShareEGuardKeys,key));
        }else if (cmd.equals(OPENKEYIEG)){//上传开门key
               String result= gSocket.UploadEGuardLog(keyieglog);
                handler.sendMessage(handler.obtainMessage(OPENKEY,result));
            }else  if (cmd.equals(WXFILL)){//没又获取到用户的头像，年龄，姓名的时候调用
            Login wxlogin=gSocket.SyncLogo(bundle.getInt("login"), bundle.getString("nickname"),bundle.getString("headimgurl"), bundle.getInt("sex",0));
            handler.sendMessage(handler.obtainMessage(WXMESSFILL,wxlogin));
        }else  if(cmd.equals(MODIHEAD)){//修改头像
            ICtrl modi = gSocket.UpdateLogo(bundle.getInt("login"), bys, bundle.getString("pathname"));
            handler.sendMessage(handler.obtainMessage(bundle.getInt("modi"),modi));
        }else if (cmd.equals(MODINAME)){//修改昵称
            ICtrl modi = gSocket.ModPerInof(bundle.getInt("login"), bundle.getString("name"), 0);
            handler.sendMessage(handler.obtainMessage(bundle.getInt("modi"),modi));

        }else if (cmd.equals(MODISEX)){//修改性别
            ICtrl modi = gSocket.ModPerInof(bundle.getInt("login"), "", bundle.getInt("sex"));
            handler.sendMessage(handler.obtainMessage(bundle.getInt("modi"),modi));
        }else if (cmd.equals(UPDATEMES)){//更新个人信息
            LoginEntity update = gSocket.getLoginInfo(bundle.getInt("loginit"));
            handler.sendMessage(handler.obtainMessage(bundle.getInt("update"),update));
        }else if (cmd.equals(LOGINMOREN)){
            String login = gSocket.Login(bundle.getString("name"), bundle.getString("pwd"));
                handler.sendMessage(handler.obtainMessage(bundle.getInt("loid"), login));
        }


    }




    /**
     * 向服务器检验是否是最新的版本
     */

    public void getVision(int star, int end) {
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, VISION);
        this.bundle.putInt("star", star);
        this.bundle.putInt("end", end);
        this.Run();
    }

    /**
     * 向服务器发送微信登录的请求
     */
    public void getWeiXinLogin(String wxname, String wxpws) {
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,WEIXINLOGIN);
        this.bundle.putString("wxname",wxname);
        this.bundle.putString("wxpws",wxpws);
        this.Run();
    }

    /**
     * 向服务器发送微信注册的请求获取验证码
     */
    public void getGainvers(String wxphotocode) {
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,WXPHOTOCODE);
        this.bundle.putString("wxphotocode",wxphotocode);
        this.Run();
    }

    /**
     * 向服务器发送微信请求验证码是否正确，正确就跳转主页面
     */
    public void getLoginCoss(String photo, String proving, String username, String nickname,Integer sex,String headimgurl,String procode) {

        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE, WXLOGINCODE);
        this.bundle.putString("photo", photo);
        this.bundle.putString("proving", proving);
        this.bundle.putString("username", username);
        this.bundle.putString("nickname", nickname);
        this.bundle.putInt("sex",sex);
        this.bundle.putString("procode", procode);
        this.bundle.putString("headimgurl",headimgurl);
        this.Run();
    }

    /**
     * 手机号登录
     */

    public void getPhotoLogin(String name, String pwd) {
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE, PHOTOLOGIN);
        this.bundle.putString("name", name);
        this.bundle.putString("pwd", pwd);
        this.Run();
    }

    /**
     * 手机号注册向服务器请求验证码
     */
    public void  getGainVersPhoto(String photo) {
       this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,VERPHOTO);
        this.bundle.putString("photo",photo);
        this.Run();
    }

    /**
     * 手机号获取验证码成功调用大乔的接口如果成功就跳转到输入密码
     */
    public void getBridPhoto(int regiid, String vercodephoto,String code_photo) {
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,BRIDPHOTO);
        this.bundle.putInt("regiid",regiid);
        this.bundle.putString("vercpdephoto",vercodephoto);
        this.bundle.putString("code_photo",code_photo);
        this.Run();
    }

    /**
     * 手机注册需要输入密码调用的接口
     */
    public void getPhotoPwd(int Loginid, String photopw, String photopws) {
       this.bundle=new Bundle();
        if (photopw!=null)
        this.bundle.putString(CMD_CODE,PHOTOPWS);
        this.bundle.putInt("Loginid",Loginid);
        this.bundle.putString("photopw",photopw);
        this.bundle.putString("photopws",photopws);
        this.Run();
    }
    /**
     * 用户在登录的时候来获取开门的key
     */
    public void getDoorKeyManage(){
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,MESSAGEDOOR);
        this.Run();
    }
    /**
     * 用户在登录的时候来获取开门的key
     */
    public void getDoorKeyManageS(){
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,MESSAGEDOORS);
        this.Run();
    }
/**
 * 获取用户的地址信息
 */
public void getLookAddress(){
    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE,LOOKADDRESS);
    this.Run();
}
    /**
     * 获取用户的授权信息
     */
    public void getAuthorAddress(){
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,AUTHORADDRESS);
        this.Run();
    }
    /**
     * 获取用户的授权信息
     */
    public void getDistrAddress(){
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,DISTRADDRESS);
        this.Run();
    }
    /**
     * 用户使用卡号认证
     */
    public void setEditCardNo(String mCardNo){
       this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,Com_Add_Carno_keys);
        this.bundle.putString("addrecar",mCardNo);
        this.Run();

    }
    /**
     * 获取用户的选的地址
     */
    public void GetGeoinfoList(IGeoinfo iGeoinfo, String type)
    {
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,Cmd_GetGeoInfo);
        this.mGeoInfo = iGeoinfo;
        this.mType = type;
        this.Run();
    }
    /**
     * 根据选好的地址获取下一个地址
     */
    public void AddAddress(String address,int gi_id)
    {
        this.bundle=new Bundle();
        this.bundle.putString(CMD_CODE,Cmd_Add_AddressforKey);
        this.bundle.putString("address", address);
        this.bundle.putInt("id", gi_id);
        this.Run();
    }

    public void RegistKeyWithIDCard(int id ,String cardNo)
    {
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE,Cmd_RegistKeyWithIDCard);
//        this.bundle.putString("name",name);
//        this.bundle.putString("card",card);
        this.mId=id;
        this.mCardNo = cardNo;
        this.Run();
    }
/**
 * 用户给别人的手机号授权
 */
public void Empower(int egkey_id,String mobile)
{
    this.mId = egkey_id;
    this.bundle = new Bundle();
    this.bundle.putString(CMD_CODE,Cmd_Empower);
    this.bundle.putString("mobile",mobile);
    this.Run();
}

/**
 * 用户分发
 */
public void ShareEGuardKey(int egkey_id, int open_total, String open_date, String expire_date,String mobile)
{
    this.mId = egkey_id;
    this.bundle = new Bundle();
    this.bundle.putString(CMD_CODE,Cmd_ShareEGuardKey);
    this.bundle.putInt("total", open_total);
    if(!open_date.isEmpty())
        this.bundle.putString("openDate", open_date);
    if(!expire_date.isEmpty())
        this.bundle.putString("expirDate",expire_date);
    this.bundle.putString("mobile",mobile);
//    this.bundle.putString("name",name);
//    this.bundle.putString("card",card);

    this.Run();
}
/**
 * 上传开门日志
 */
public void getOpenKey(IEGLogCtrl ieglog){
    keyieglog=ieglog;
    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE,OPENKEYIEG);
    this.Run();

}

/**
 * 如果用户没有上传头像、年龄、姓名调用此接口
 */
public void getFill(int login_id, String nickname,Integer sex,String headimgurl){
    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE, WXFILL);
    this.bundle.putInt("login",login_id);
    this.bundle.putString("nickname", nickname);
    this.bundle.putInt("sex",sex);
    this.bundle.putString("headimgurl",headimgurl);
    this.Run();
}


/**
 * 修改用户的头像
 */
public void setModiHead(int loginid ,byte by[],String pathname,int modi){
    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE, MODIHEAD);
    this.bundle.putInt("login",loginid);
    this.bundle.putString("pathname", pathname);
    this.bundle.putInt("modi",modi);
    this.bys=by;
    this.Run();
    
    
    
    
    
    
}

/**
 * 修改用户的昵称
 */
public void setModiname(int loginid,String name,int modi){
    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE, MODINAME);
    this.bundle.putInt("login",loginid);
    this.bundle.putString("name", name);
    this.bundle.putInt("modi",modi);

    this.Run();




}
/**
 * 修改用户的性别
 */
public void setModiSex(int loginid,String name,int sex,int modi){
    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE, MODISEX);
    this.bundle.putInt("login",loginid);
    this.bundle.putInt("sex", sex);
    this.bundle.putInt("modi",modi);
    this.Run();

}

/**
 * 更新个人信息
 */
public void setUpdateMessage(int loginit,int update){

    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE, UPDATEMES);
    this.bundle.putInt("loginit", loginit);
    this.bundle.putInt("update",update);
    this.Run();


}
/**
 * 进入app默认登录
 */

public void Login(String name ,String pwd,int loid){
    this.bundle=new Bundle();
    this.bundle.putString(CMD_CODE, LOGINMOREN);
    this.bundle.putString("name", name);
    this.bundle.putString("pwd", pwd);
    this.bundle.putInt("loid", loid);
    this.Run();

}


}
